<?php

namespace app\home\controller;

use app\admin\model\company\Example;
use app\admin\model\company\ExampleIndustry;
use app\admin\model\company\ExampleType;
use app\admin\model\company\Product;
use app\admin\model\company\Setting;
use app\admin\model\company\Share;
use app\admin\model\company\ShareCollection;
use app\admin\model\company\ShareType;
use app\common\controller\Frontend;

class Page extends Frontend
{

    protected $noNeedLogin = '*';
    protected $noNeedRight = '*';
    protected $layout = '';


    public function _initialize()
    {
        $this->assign('currentUrl', request()->url());
        parent::_initialize(); // TODO: Change the autogenerated stub
    }

    public function customWebsite()
    {
        $shareIds = Setting::getValueByKey('website_share');
        $shares = Share::all($shareIds);

        $this->assign('shares', $shares);

        $pcExampleIds = Setting::getValueByKey('website_example');
        $pxExamples = Example::all($pcExampleIds);

        //案例第一块
        $this->assign('pcExamples', $pxExamples);
        //案例第二块
        shuffle($pxExamples);
        $this->assign('tow_pcExamples', $pxExamples);


        $industries = ExampleIndustry::all(null, ['examples']);
        $renderIndustries = [];
        foreach ($industries as $item) {
            if (count($item->examples) > 0) {
                $renderIndustries[] = [
                    'id' => $item->id,
                    'name' => $item->industry_name,
                    'count' => count($item->examples),
                ];
            }
        }
        $renderIndustries = array_slice($renderIndustries, 0, 10);
        $this->assign('renderIndustries', $renderIndustries);

        return $this->view->fetch();
    }

    public function customMiniapp()
    {
        $shareIds = Setting::getValueByKey('miniprogram_share');
        $shares = Share::all($shareIds);

        $this->assign('shares', $shares);

        $exampleIds = Setting::getValueByKey('miniprogram_example');
        $examples = Example::all($exampleIds);

        //案例第一块
        $this->assign('examples', $examples);
        //案例第二块
        shuffle($examples);
        $this->assign('tow_examples', $examples);


        $industries = ExampleIndustry::all(null, ['examples']);
        $renderIndustries = [];
        foreach ($industries as $item) {
            if (count($item->examples) > 0) {
                $renderIndustries[] = [
                    'id' => $item->id,
                    'name' => $item->industry_name,
                    'count' => count($item->examples),
                ];
            }
        }
        $renderIndustries = array_slice($renderIndustries, 0, 10);
        $this->assign('renderIndustries', $renderIndustries);

        return $this->view->fetch();
    }

    public function product()
    {
        $products = Product::all();
        $this->assign('products', $products);
        return $this->view->fetch();
    }

    public function productDetail()
    {
        $product = Product::get(input('id'));
        $this->assign('product', $product);
        return $this->view->fetch();
    }

    public function case()
    {
        $page = input('page', 1);
        $limit = 9;

        $type = $this->request->get('type', 0);
        $industry = $this->request->get('industry', 0);

        $types = ExampleType::all();
        $this->assign('types', $types);

        $industries = ExampleIndustry::all();
        $this->assign('industries', $industries);

        $model = new Example();
        if ($type) {
            $model->where('example_type_id', $type);
        }
        if ($industry) {
            $model->where('example_industry_id', $industry);
        }
        $list = $model->order('id', 'desc')->paginate($limit, false, ['page' => $page]);
        $data = $list->toArray();

        $this->assign('examples', $data['data']);

        return $this->view->fetch();
    }

    public function caseDetail()
    {
        $example = Example::get(input('id'));
        $this->assign('example', $example);
        return $this->view->fetch();
    }

    public function ajaxCaseList()
    {
        $page = input('page', 2);
        $limit = 9;

        $type = $this->request->get('type', 0);
        $industry = $this->request->get('industry', 0);

        $types = ExampleType::all();
        $this->assign('types', $types);

        $industries = ExampleIndustry::all();
        $this->assign('industries', $industries);

        $model = new Example();
        if ($type) {
            $model->where('example_type_id', $type);
        }
        if ($industry) {
            $model->where('example_industry_id', $industry);
        }
        $list = $model->order('id', 'desc')->paginate($limit, false, ['page' => $page]);
        $data = $list->toArray();

        $data['data'] = array_map(function ($e) {
            return array_merge($e, [
                'coverimage' => cdnurl($e['coverimage'], true),
                'cover5v4image' => cdnurl($e['cover5v4image'], true),
                'cover1v1image' => cdnurl($e['cover1v1image'], true),
                'type' => $e['example_type_id'] == 1 ? 'pc' : 'mini',
            ]);
        }, $data['data']);
        echo json_encode($data);
        exit;
    }

    public function knowledgeShare()
    {
        $page = input('page', 1);
        $limit = 6;
        $type = $this->request->get('type', 0);
        $collection = $this->request->get('collection', 0);

        $types = ShareType::all();
        $this->assign('types', $types);

        $collections = ShareCollection::all();
        $this->assign('collections', $collections);

        $model = new Share();
        if ($type) {
            $model->where('share_type_id', $type);
        }
        if ($collection) {
            $model->where('share_collection_id', $collection);
        }
        $list = $model->with(['sharetype', 'sharecollection'])->order('id', 'desc')->paginate($limit, false, ['page' => $page]);
        $data = $list->toArray();

        $this->assign('shares', $data['data']);

        return $this->view->fetch();
    }

    public function knowledgeShareDetail()
    {
        $share = Share::get(input('id'));
        $this->assign('share', $share);
        return $this->view->fetch();
    }

    public function ajaxKnowledgeShare()
    {
        $page = input('page', 2);
        $limit = 6;
        $type = $this->request->get('type', 0);
        $collection = $this->request->get('collection', 0);

        $types = ShareType::all();
        $this->assign('types', $types);

        $collections = ShareCollection::all();
        $this->assign('collections', $collections);

        $model = new Share();
        if ($type) {
            $model->where('share_type_id', $type);
        }
        if ($collection) {
            $model->where('share_collection_id', $collection);
        }
        $list = $model->with(['sharetype', 'sharecollection'])->order('id', 'desc')->paginate($limit, false, ['page' => $page]);
        $data = $list->toArray();

        $data['data'] = array_map(function ($e) {
            return array_merge($e, [
                'coverimage' => cdnurl($e['coverimage'], true),
                'cover5v4image' => cdnurl($e['cover5v4image'], true),
                'cover1v1image' => cdnurl($e['cover1v1image'], true),
            ]);
        }, $data['data']);
        echo json_encode($data);
        exit;
    }


    public function contactus()
    {
        $wechat = Setting::getValueByKey('weixin');
        $address = Setting::getValueByKey('address');
        $wechatqrcode = Setting::getValueByKey('weixin_image');
        $gzhqrcode = Setting::getValueByKey('gzh_image');
        $gzh = Setting::getValueByKey('gzh');
        $this->assign('wechat', $wechat);
        $this->assign('address', $address);
        $this->assign('wechatqrcode', cdnurl($wechatqrcode, true));
        $this->assign('gzhqrcode', cdnurl($gzhqrcode, true));
        $this->assign('gzh', $gzh);
        return $this->view->fetch();
    }

}
